package com.resolution.dietaryassessment.view.chronic.model;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;

import com.resolution.dietaryassessment.R;

import java.util.Objects;


/**
 *  @author By Resolusion
 *  @Title: TimeAxisView
 *  @Date: 2020/3/23
 *  @Time: 13:11
 *  @email res01ution@foxmail.com
 */
public class ProgressPointView extends View {
    private static final String TAG = "TimeAxisView";

    public static final int HOLLOW_CIRCLE = 0;
    public static final int CENTER_CIRCLE = 1;

    public static final int GRAVITY_VERTICAL = 1;
    public static final int GRAVITY_HORIZONTAL = -1;

    public static final int WAIT_TYPE = 0;
    public static final int RIGHT_TYPE = 1;
    public static final int ERROR_TYPE = 2;

    private Paint mLinePaint; // 线画笔
    private Paint mPointPaint; // 点画笔

    private float mLineWidth = 5; //线宽
    private int mLineColor;     //线得颜色
    private float mCircleRadius = 0; //圆圈半径
    private int mPointColor;

    // 中心圆类型
    private int CIRCLE_SHAPE;
    //中心圆点颜色
    private int CIRCLE_COLOR;
    // 横轴or竖轴
    private int GRAVITY = GRAVITY_HORIZONTAL;

    private boolean isHeadPoint = false;
    private boolean isFootPoint = false;

    private float start; //画线轴得起点

    private int defaultSize;

    public ProgressPointView(Context context) {
        this(context, null);
    }

    public ProgressPointView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ProgressPointView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.ProgressPointView);
        try {
            mLineWidth = ta.getDimension(R.styleable.ProgressPointView_line_width, 5);
            mLineColor = ta.getColor(R.styleable.ProgressPointView_line_color, getResources().getColor(R.color.colorLine));
            mCircleRadius = ta.getResourceId(R.styleable.ProgressPointView_point_size, 0);
            mPointColor = ta.getColor(R.styleable.ProgressPointView_point_color, getResources().getColor(R.color.colorLine));
        }finally {
            ta.recycle();
        }
        initView();
    }

    private void initView() {
        mLinePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mLinePaint.setStyle(Paint.Style.FILL);

        mPointPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        Log.d(TAG, "initView: " + getScreenWidth());
    }

    /**
     * 获取屏幕宽度
     */
    private int getScreenWidth() {
        WindowManager windowManager = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics displayMetrics = new DisplayMetrics();
        Objects.requireNonNull(windowManager).getDefaultDisplay().getMetrics(displayMetrics);
        return displayMetrics.widthPixels;
    }

    /**
     * 获取视图大小
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(getProperSize(widthMeasureSpec), getProperSize(heightMeasureSpec));
    }

    /**
     * 获取view的宽度
     */
    private int getProperSize( int measureSpec){
        defaultSize = (int)(getScreenWidth() - 123)/ 25;
        Log.d(TAG, "getProperSize: >>" + start);
        int properSize = defaultSize;
        int mode = MeasureSpec.getMode(measureSpec);
        int size = MeasureSpec.getSize(measureSpec);
        switch (mode) {
            case MeasureSpec.UNSPECIFIED:
                // 没有指定大小，设置为默认大小
                properSize = defaultSize;
                break;
            case MeasureSpec.EXACTLY:
                // 固定大小，无需更改其大小
                properSize = size;
                break;
            case MeasureSpec.AT_MOST:
                // 此处该值可以取小于等于最大值的任意值，此处取最大值的1/4
                properSize = size / 4;
                break;
            default:
        }
        return properSize;
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Log.d(TAG, "onDraw: "+getHeight()+"..."+getWidth());
        if (mCircleRadius == 0){
            mCircleRadius = mLineWidth * 2;
        }
        Gravity();
        mLinePaint.setColor(mLineColor);
        mLinePaint.setStrokeWidth(mLineWidth);


        if (!isHeadPoint){
            canvas.drawLine(0, start, (float)getWidth()/2 - mCircleRadius, start, mLinePaint);
        }
        if (!isFootPoint) {
            canvas.drawLine((float)getWidth()/2 + mCircleRadius, start, getWidth(), start,  mLinePaint);
        }

        switch (CIRCLE_SHAPE){
            case HOLLOW_CIRCLE:
                mPointPaint.setStyle(Paint.Style.STROKE);
                break;
            case CENTER_CIRCLE:
                mPointPaint.setStyle(Paint.Style.FILL);
                mPointPaint.setStrokeWidth(mLineWidth/2);
                getCircleColorType();
                canvas.drawCircle((float)getWidth()/2, getHeight()/2, mCircleRadius/2, mPointPaint);
                mPointPaint.setStyle(Paint.Style.STROKE);
                break;
        }
        mPointPaint.setColor(mPointColor);
        mPointPaint.setStrokeWidth(mLineWidth/2);
        canvas.drawCircle((float)getWidth()/2, getHeight()/2, mCircleRadius, mPointPaint);
    }


    /**
     * 根据圆的状态，赋予不同颜色
     */
    private void getCircleColorType(){
        switch (CIRCLE_COLOR) {
            case WAIT_TYPE:
                mPointPaint.setColor(getResources().getColor(R.color.colorWait));
                break;
            case RIGHT_TYPE:
                mPointPaint.setColor( getResources().getColor(R.color.colorR));
                break;
            case ERROR_TYPE:
                mPointPaint.setColor( getResources().getColor(R.color.colorE));
                break;
        }
    }

    /**
     * 设置View的方向
     */
    private void Gravity(){
        switch (GRAVITY){
            case GRAVITY_VERTICAL:
                start = getWidth() - mLineWidth -mCircleRadius;
                break;
            case GRAVITY_HORIZONTAL:
                start = (getHeight() - mLineWidth)/2 + mCircleRadius/4;
                Log.d(TAG, "Gravity: " + start);
                break;
        }
    }

    /**
     * 设置线宽
     * @param width 直线轴宽
     */
    public void setLineWidth(float width){
        this.mLineWidth = width;
        update();
    }

    /**
     * 设置线颜色
     * @param resourceId 直线轴颜色
     */
    public void setLineColor(int resourceId){
        this.mLineColor = getResources().getColor(resourceId);
        update();
    }

    /**
     * 设置圆圈类型
     * @param shape  中心圆类型
     */
    public void setCircleShape(int shape){
        this.CIRCLE_SHAPE = shape;
        update();
    }

    /**
     * 设置中心圆颜色
     */
    public void setCircleColor(int colorID){
        this.CIRCLE_COLOR = colorID;
        update();
    }

    public void setDefaultSize(int size){
        this.defaultSize = size;
        update();
    }

    /**
     * 头部关键点
     */
    public void isHeadView(boolean isHead){
        this.isHeadPoint = isHead;
        update();
    }

    /**
     * 足部关键点
     */
    public void isFootView(boolean isFoot){
        this.isFootPoint = isFoot;
        update();
    }

    /**
     *  横竖展示
     */
    private void setGravity(int gravity){
        this.GRAVITY = gravity;
    }

    /**
     *  设置圆点颜色
     */
    public void setPointColor(int resourceId){
        this.mPointColor = getResources().getColor(resourceId);
        update();
    }

    /**
     *  设置圆圆半径
     */
    public void setPointRadius(float size){
        this.mCircleRadius = size;
        update();
    }

    private void update(){
        this.invalidate();
    }
}
